from pydantic_settings import BaseSettings
from pydantic import EmailStr
from typing import Optional
import os
from pathlib import Path

# 获取项目根目录
BASE_DIR = Path(__file__).resolve().parent.parent.parent

class Settings(BaseSettings):
    EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
    EMAILS_ENABLED: bool = False
    EMAILS_FROM_EMAIL: Optional[EmailStr] = None
    EMAILS_FROM_NAME: Optional[str] = None
    EMAIL_TEMPLATES_DIR: Path = Path(__file__).parent.parent / "email-templates"

    # SMTP settings
    SMTP_TLS: bool = True
    SMTP_PORT: Optional[int] = 465
    SMTP_HOST: Optional[str] = 'smtp.163.com'
    SMTP_USER: Optional[str] = 'chydroid@163.com'
    SMTP_PASSWORD: Optional[str] = 'Chyxwt32326588'

    # Server settings
    SERVER_HOST: str = "http://localhost:8000"
    PROJECT_NAME: str = "EduCool"
    VERSION: str = "1.0.0"
    API_V1_STR: str = "/api/v1"

    # SQLite配置
    SQLITE_DB: str = "educool.db"
    SQLALCHEMY_DATABASE_URI: str = f"sqlite:///{BASE_DIR}/{SQLITE_DB}"

    # JWT配置
    SECRET_KEY: str = os.getenv("SECRET_KEY", "your-secret-key-here")
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30

    # Redis配置（如果需要的话）
    REDIS_HOST: str = os.getenv("REDIS_HOST", "localhost")
    REDIS_PORT: int = int(os.getenv("REDIS_PORT", 6379))

    # OpenAI配置
    OPENAI_API_KEY: Optional[str] = os.getenv("OPENAI_API_KEY")

    class Config:
        case_sensitive = True

settings = Settings()